<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>04_迭代对象-可迭代对象优化</title>
</head>

<body>

  <script>

    // 可迭代对象优化 key/value
    const infos = {
      name: 'entries',
      age: 18,
      friends: ['张三', '李四', '王五'],
      [Symbol.iterator]() {
        const entries = Object.entries(this);
        return {
          next() {
            this.index = this.index || 0;
            if (this.index === entries.length) {
              return {
                value: undefined,
                done: true
              }
            }
            return {
              value: entries[this.index++],
              done: false
            }
          }
        }
      }
    }

    // 可迭代对象必然具备下面的特点
    const iterator = infos[Symbol.iterator]()
    console.log(iterator.next())
    console.log(iterator.next())
    console.log(iterator.next())
    console.log(iterator.next())

    // 可迭对象可以进行for of操作
    for (const item of infos) {
      const [key, value] = item
      console.log(key, value)
    }


  </script>

</body>

</html>